package com.zx.server.api.impl;

import com.zx.server.api.UserServer;
import com.zx.server.api.pojo.PageReq;
import com.zx.server.api.pojo.UserInfoReq;
import com.zx.server.api.pojo.UserInfoRes;
import com.zx.server.entity.ContactEntity;
import com.zx.server.entity.IdentityEntity;
import com.zx.server.entity.UserEntity;
import com.zx.server.fengin.ContactServerFenign;
import com.zx.server.fengin.IdentityServerFenign;
import com.zx.server.service.UserService;
import com.zx.server.util.ResultHelper;
import com.zx.server.util.ServiceCode;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.zx.server.util.ResponseModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther: zhangxin
 * @Description:
 */
@RestController
public class UserServerImpl implements UserServer {


    @Autowired
    private UserService userService;

    @Autowired
    private ContactServerFenign contactServerFenign;

    @Autowired
    private IdentityServerFenign identityServerFenign;

    @Override
    @RequestMapping("/addUser")
    public ResponseEntity<ResponseModel> addUser(@RequestBody UserInfoReq userInfoReq) {
        UserEntity user = new UserEntity();
        IdentityEntity identity = new IdentityEntity();
        ContactEntity contact = new ContactEntity();

        try {
            HashMap<String, Object> returnMap = new HashMap<>();
            user.setName(userInfoReq.getName());
            user.setSex(userInfoReq.getSex());
            user.setIdentityStatus("未认证");
            identity.setRealName(userInfoReq.getName());
            identity.setIdNo(userInfoReq.getIdNo());
            identity.setIdentityStatus("未认证");
            contact.setName(userInfoReq.getName());
            contact.setPhone(userInfoReq.getPhone());
            contact.setEmail(userInfoReq.getEmail());
            int num1 = userService.addUser(user);
            int num2 = identityServerFenign.addIdentity(identity);
            int num3 = contactServerFenign.addContact(contact);
            returnMap.put("return", "创建成功");
            return new ResponseEntity<ResponseModel>(ResultHelper.createOKResult(returnMap), HttpStatus.OK);
        }catch (Exception e) {
            return new ResponseEntity<ResponseModel>(
                    ResultHelper.createResult(ServiceCode.REQUEST_SERVER_FAIL, "服务器请求异常"), HttpStatus.OK);
        }
    }

    @Override
    @RequestMapping("/getUserInfoList")
    @Transactional
    @GlobalTransactional(rollbackFor = Exception.class)
    public ResponseEntity<ResponseModel> getUserInfoList(@RequestBody PageReq req) {


        try {
            HashMap<String, Object> returnMap = new HashMap<>();
            List<IdentityEntity> identityList = identityServerFenign.getIdentityList(req.getKey(),req.getLimit(),req.getOffset());

            if (null == identityList) {
                return new ResponseEntity<ResponseModel>(ResultHelper.createResult(ServiceCode.NO_DATA, "未查询到符合条件的数据"),  HttpStatus.OK);
            }
           //获取id集合
            List<Integer> ids = identityList.stream().map(IdentityEntity::getId).collect(Collectors.toList());
            List<UserEntity> userList = userService.getUserList(ids);

            if(null == userList){
                return new ResponseEntity<ResponseModel>(ResultHelper.createResult(ServiceCode.NO_DATA, "未查询到符合条件的数据"),  HttpStatus.OK);
            }

            List<ContactEntity> contactEntityList = contactServerFenign.getContactList(ids);

            ArrayList<UserInfoRes> userInfoResList = new ArrayList<>();
            for (int i = 0; i < identityList.size(); i++) {
                UserInfoRes userInfoRes = new UserInfoRes();
                userInfoRes.setId(userList.get(i).getId());
                userInfoRes.setName(userList.get(i).getName());
                userInfoRes.setSex(userList.get(i).getSex());
                userInfoRes.setIdNo(identityList.get(i).getIdNo());
                userInfoRes.setIdentityStatus(identityList.get(i).getIdentityStatus());
                userInfoRes.setPhone(contactEntityList.get(i).getPhone());
                userInfoRes.setEmail(contactEntityList.get(i).getEmail());
                userInfoResList.add(userInfoRes);
            }
            returnMap.put("return", userInfoResList);
            return new ResponseEntity<ResponseModel>(ResultHelper.createOKResult(returnMap), HttpStatus.OK);

        }catch (Exception e) {
            return new ResponseEntity<ResponseModel>(
                    ResultHelper.createResult(ServiceCode.REQUEST_SERVER_FAIL, "服务器请求异常"), HttpStatus.OK);
        }

    }




}
